home *** CD-ROM | disk | FTP | other *** search
- /*
- * draw.c - window handling for the project critic.
- *
- */
-
- #include <memory.h>
- #include <quickdraw.h>
- #include <window.h>
- #include <osutil.h>
- #include <event.h>
- #include <toolutil.h>
- #include <resource.h>
- #include <segment.h>
-
- #include "progerr.h"
- #include "critic.h"
-
- /*
- * BASEPICT - resource ID of the first Pict to get.
- * Pict numbers are sequential, in the following order:
- * Facing right, frame 1
- * Facing right, frame 2
- * Facing left, frame 1
- * Facing left, frame 2
- */
-
- #define BASEPICT 256
-
- #define NUM_DIRS 2 /* directions: 0 == to the right; 1 == opposite */
- #define FRAMES_PER 2 /* frames per direction */
-
- PicHandle apict[NUM_DIRS][FRAMES_PER]; /* pictures to draw */
-
- /*
- * vhinc[] - for each direction, the vert and horz movement per frame.
- */
-
- Point vhinc[NUM_DIRS] = {
- {0, 12},
- {0, -12}
- };
-
- BitMap offscreen; /* off-screen bitmap for cheap animation */
-
- int curdir; /* current direction of movement */
- int curframe; /* current frame for this direction */
- Rect currect; /* rect surrounding the current pict */
-
- /*
- * doc_init() - initialize the data needed to draw the screen.
- * Get all of the pictures to draw,
- * allocate the offscreen bitmap and draw into it.
- */
-
- doc_init()
- {
- int resid; /* Resource ID of the current Pict to get */
- BitMap oldmap; /* windoc's previous bitmap */
- RgnHandle oldvis; /* windoc's previous visible region */
- GrafPtr saveport;
-
- topLeft(offscreen.bounds) = topLeft(windoc->portBits.bounds);
- botRight(offscreen.bounds) = botRight(windoc->portBits.bounds);
- offscreen.rowBytes = windoc->portBits.rowBytes;
-
- offscreen.baseAddr = (QDPtr)
- NewPtr((Size) ((int) offscreen.rowBytes *
- (offscreen.bounds.bottom - offscreen.bounds.top)));
- if (!offscreen.baseAddr) {
- progstop(PE_NOMEM);
- ExitToShell();
- }
-
- /*
- * get handles for the background pictures
- */
-
- resid = BASEPICT;
- for (curdir = 0; curdir < NUM_DIRS; ++curdir) {
- for (curframe = 0; curframe < FRAMES_PER; ++curframe) {
- apict[curdir][curframe] =
- (PicHandle) GetResource((long) 'PICT', resid);
- ++resid;
- }
- }
-
- /*
- * Set the initial state, then draw it into the bitmap.
- */
-
- curdir = 0;
- curframe = 0;
-
- LoadResource(apict[curdir][curframe]);
- topLeft(currect) = topLeft((*apict[curdir][curframe])->picFrame);
- botRight(currect) = botRight((*apict[curdir][curframe])->picFrame);
- OffsetRect(&currect, -currect.right, -currect.top);
-
- GetPort(&saveport);
- SetPort(windoc);
- oldmap.baseAddr = thePort->portBits.baseAddr;
- oldmap.rowBytes = thePort->portBits.rowBytes;
- oldmap.bounds = thePort->portBits.bounds;
- SetPortBits(&offscreen);
- oldvis = NewRgn();
- CopyRgn(thePort->visRgn, oldvis);
- CopyRgn(thePort->clipRgn, thePort->visRgn);
-
- EraseRect(&(thePort->portRect));
- LoadResource(apict[curdir][curframe]);
- DrawPicture(apict[curdir][curframe], &currect);
-
- CopyRgn(oldvis, thePort->visRgn);
- DisposeRgn(oldvis);
- SetPortBits(&oldmap);
- SetPort(saveport);
- }
-
- /*
- * redoc() - redraw the contents of our window, windoc.
- */
-
- redoc()
- {
- GrafPtr saveport;
-
- BeginUpdate(windoc);
- GetPort(&saveport);
- SetPort(windoc);
-
- CopyBits(&offscreen, &(thePort->portBits),
- &(thePort->portRect), &(thePort->portRect),
- srcCopy, (RgnHandle) 0);
-
- SetPort(saveport);
- EndUpdate(windoc);
- }
-
- /*
- * draw_idle() - redraw the screen as necessary in an idle loop
- */
-
- draw_idle()
- {
- #define ANIMTIME 6 /* ticks per frame */
- static unsigned long drawtime = 0L; /* next time to update the screen */
- unsigned long nowtime; /* the current time */
- GrafPtr saveport;
- BitMap oldmap; /* the original version of windoc's bitmap */
- RgnHandle oldvis; /* windoc's previous visible region */
-
- if (drawtime == 0L) drawtime = TickCount();
-
- nowtime = TickCount();
- if (nowtime < drawtime - 1) {
- return;
- }
-
- /*
- * Prepare to draw into the offscreen bitmap:
- * Select our window;
- * Set our window's bitmap to the offscreen one;
- * Set our window's visible region to the whole window.
- */
-
- GetPort(&saveport);
- SetPort(windoc);
- oldmap.baseAddr = thePort->portBits.baseAddr;
- oldmap.rowBytes = thePort->portBits.rowBytes;
- oldmap.bounds = thePort->portBits.bounds;
- SetPortBits(&offscreen);
- oldvis = NewRgn();
- CopyRgn(thePort->visRgn, oldvis);
- CopyRgn(thePort->clipRgn, thePort->visRgn);
-
- EraseRect(&currect);
-
- /*
- * calculate the new state of the screen,
- * draw it into the offscreen bitmap.
- */
-
- if (++curframe >= FRAMES_PER) {
- curframe = 0;
- }
- if (curdir == 0) {
- if (currect.left >= windoc->portRect.right) {
- curdir = 1;
- }
- } else {
- if (currect.right < windoc->portRect.left) {
- curdir = 0;
- }
- }
- OffsetRect(&currect, vhinc[curdir].h, vhinc[curdir].v);
-
- LoadResource(apict[curdir][curframe]);
- DrawPicture(apict[curdir][curframe], &currect);
-
- /*
- * Restore our window to what it was.
- */
-
- CopyRgn(oldvis, thePort->visRgn);
- DisposeRgn(oldvis);
- SetPortBits(&oldmap);
-
- /*
- * "quickly" (oink, oink!) redraw the screen.
- */
-
- CopyBits(&offscreen, &(thePort->portBits), &(thePort->portRect),
- &(thePort->portRect), srcCopy, (RgnHandle) 0);
-
- drawtime = TickCount() + ANIMTIME;
- SetPort(saveport);
- }
-